/* 
 * File:   SVMPredictor.h
 * Author: kiennd
 *
 * Created on January 10, 2014, 3:13 PM
 * 
 * C++ wrapper for svm-predict.
 * 
 * NOTE: No thread-safe warranty
 * 
 * USAGE:
 *  + set predict probability and quite mode (default is false, false)
 *  + load model
 *  + call predict methods
 * 
 */

#ifndef SVMPREDICTOR_H
#define	SVMPREDICTOR_H

#include <vector>

#include "svm.h"

using namespace std;

class SVMPredictor {
public:
	const static bool kDefaultPredictProbability = false;

	/**
	 * Initialize predictor with kDefaultPredictProbability, 
	 * not quiet and no model loaded
	 */
	SVMPredictor();
	~SVMPredictor();

	/**
	 * Load a model from file. 
	 * Destroy previously loaded model
	 * @param modelFile model file
	 * @return whether model is loaded successfully or not
	 */
	bool loadModel(const char* modelFile);

	/**
	 * Destroy model if loaded before
	 */
	void destroyModel();

	bool isPredictProbability();
	void setPredictProbability(bool isPredict);

	/**
	 * Whether to print running information or not
	 * @param isQuiet true means not printing running information
	 */
	void setQuietMode(bool isQuiet);

	/**
	 * Parse data from a line
	 * @param targetLabel parsed target label
	 * @param data parsed data
	 * @param line input line
	 * @param currentLineNumber current line number, only used for error logging
	 * @return whether parsing successfully of not
	 */
	bool parseLineData(double& targetLabel, vector<svm_node>& data, string& line, int currentLineNumber);

	/**
	 * Predict data in input file
	 */
	void predictFile(const char* input, const char *outputFile);

	/**
	 * Predict a data point
	 * @return predicted label
	 */
	double predictData(const vector<svm_node>& data, vector<double>& probabilityEstimates);

private:
	struct svm_model* model;

	bool predictProbability;

	bool modelLoaded;

	int (*info)(const char *fmt, ...);

	static int print_null(const char *s, ...) {
		return 0;
	}
};

#endif	/* SVMPREDICTOR_H */

